﻿//------------------------------------------------------------------------------ 
// <copyright file="JQueryProxyManager.cs" company="Fm Computer">
//     Copyright (c) Fm Computer di Menatti Fulvia.  All rights reserved.
// </copyright>
//----------------------------------------------------------------------------- 
namespace FmjQuery
{
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    /// <summary>
    /// Proxy jQuery Manager
    /// </summary>
    public class JQueryProxyManager : Control, IjQueryManager, IManageBaseScript, IManageEffects
    {
        #region Fields
        /// <summary>
        /// JQuery Manager
        /// </summary>
        private JQueryManager tmanager;

        /// <summary>
        /// Theme for jquery ui component
        /// </summary>
        private Themes ttheme;
        #endregion

        #region Property IManageBaseScript
        /// <summary>
        /// Gets a value indicating whether insert into page jQuery FrameWork
        /// </summary>
        public bool FrameWork
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether insert into page jQuery UI FrameWork
        /// </summary>
        public bool UiFrameWork
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether insert into page jQuery UI Tabs
        /// </summary>
        public bool Tab
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether insert into page jQuery UI Accordion
        /// </summary>
        public bool Accordion
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether insert into page jQuery Extension FrameWork
        /// </summary>
        public bool FrameWorkExtend
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether insert into page DotNetService call
        /// </summary>
        public bool DotNetService
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether insert into page JSon
        /// </summary>
        public bool JSon
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether insert into page ModalPopUp
        /// </summary>
        public bool ModalPopUp
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether insert into page AutoComplate
        /// </summary>
        public bool AutoComplete
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether insert into page innerFade
        /// </summary>
        public bool InnerFade
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether insert into page Draggable
        /// </summary>
        public bool Draggable
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether insert into page Alert
        /// </summary>
        public bool Alert
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether insert into page Edit Mask
        /// </summary>
        public bool EditMask
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether insert into page Validator
        /// </summary>
        public bool Validator
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether insert into page datepicker
        /// </summary>
        public bool DatePicker
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether manage culture (languages)
        /// </summary>
        public bool ManageCulture
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether insert into page bgframe
        /// </summary>
        public bool BgFrame
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether insert into page resizable
        /// </summary>
        public bool Resizable
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether insert into page dialog
        /// </summary>
        public bool Dialog
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether insert into page Droppable
        /// </summary>
        public bool Droppable
        {
            get;
            set;
        }
        #endregion

        #region Implementation of IManageEffects
        /// <summary>
        /// Gets or sets a value indicating whether insert easing effect script
        /// </summary>
        public bool Easing
        {
            get;
            set;
        }
        #endregion

        #region Property
        /// <summary>
        /// Gets JQueryManager
        /// </summary>
        public JQueryManager Manager
        {
            get
            {
                if (this.tmanager == null)
                {
                    this.tmanager = JQueryManager.GetCurrent(Page);
                }

                return this.tmanager;
            }
        }

        /// <summary>
        /// Gets or sets theme for jquery ui component
        /// </summary>
        public Themes Theme
        {
            get
            {
                return this.ttheme;   
            }

            set
            {
                this.ttheme = value;

                if (this.Manager != null)
                {
                    this.ChangeTheme(value);
                }
            }
        }

        /// <summary>
        /// Gets list of resource that include into page
        /// </summary>
        public JWebSources WebResources
        {
            get
            {
                return (this.Manager == null) ? null : this.Manager.WebResources;
            }
        }
        #endregion

        #region Method
        /// <summary>
        /// Register resource that need control
        /// </summary>
        /// <param name="control">extend control</param>
        public void RegisterResources(JQueryBaseControl control)
        {
            this.Manager.RegisterResources(control);
        }

        /// <summary>
        /// Chenge theme
        /// </summary>
        /// <param name="theme">new theme that apply to page</param>
        public void ChangeTheme(Themes theme)
        {
            this.Manager.ChangeTheme(theme);
        }

        #endregion

        #region Override
        /// <summary>
        /// Register Base script
        /// </summary>
        /// <param name="e">argument of event</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this.Manager.UiFrameWork = this.UiFrameWork;
            this.Manager.AutoComplete = this.AutoComplete;
            this.Manager.DotNetService = this.DotNetService;
            this.Manager.FrameWorkExtend = this.FrameWorkExtend;
            this.Manager.InnerFade = this.InnerFade;
            this.Manager.JSon = this.JSon;
            this.Manager.ModalPopUp = this.ModalPopUp;
            this.Manager.Draggable = this.Draggable;
            this.Manager.Tab = this.Tab;
            this.Manager.Accordion = this.Accordion;
            this.Manager.Alert = this.Alert;
            this.Manager.EditMask = this.EditMask;
            this.Manager.Validator = this.Validator;
            this.Manager.ChangeTheme(this.Theme);
            this.Manager.Easing = this.Easing;
            this.Manager.DatePicker = this.DatePicker;
            this.Manager.ManageCulture = this.ManageCulture;
            this.Manager.BgFrame = this.BgFrame;
            this.Manager.Resizable = this.Resizable;
            this.Manager.Dialog = this.Dialog;
            this.Manager.Droppable = this.Droppable;
        }

        /// <summary>
        /// Remove render of control
        /// </summary>
        /// <param name="writer">Html Text Writer</param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (DesignMode)
            {
                Literal lt = new Literal { Text = string.Format("jQueryControl - ID : {0} - Type : {1}", this.ID, this.GetType().Name) };
                lt.RenderControl(writer);
            }
        }
        #endregion
    }
}
